package com.pisolution.scheduler.runtime;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;

/**
 * 该类主要负责传递相应的实例给那些无法使用注解方式载入实体对象的线程.
 *
 * @author sora
 * @version 0.1.0
 */
@Component
public class SchedulerContext implements ApplicationContextAware {
    /** 应用上下文. */
    private static ApplicationContext context;

    // -------------------------------------------------------------------------------------------------
    //  属性访问
    // -------------------------------------------------------------------------------------------------

    @Override
    public void setApplicationContext(@Nonnull ApplicationContext context) throws BeansException {
        SchedulerContext.context = context;
    }

    // -------------------------------------------------------------------------------------------------
    //  实体载入
    // -------------------------------------------------------------------------------------------------

    /**
     * 获取给定类型的实体对象 该对象为可注入类型.
     *
     * @param type 可注入对象类型.
     * @param <T> 该对象所属的类型.
     *
     * @return 已经实例化的相应类型的对象.
     */
    public static <T> T get(Class<T> type) {
        return context.getBean(type);
    }
}
